<?php
/**
 * @copyright  Copyright  LexisNexis, a division of Reed Elsevier Inc. All rights reserved.
 */
require_once __DIR__ . '/vendor/autoload.php';
$_ENV = array();
$dotenv = new Dotenv\Dotenv(__DIR__);
$dotenv->load();

define('MQ_HOST', UsageConfig::env('MQ_HOST'));
define('MQ_PORT', UsageConfig::env('MQ_PORT'));
define('MQ_USER', UsageConfig::env('MQ_USER'));
define('MQ_PASS', UsageConfig::env('MQ_PASS'));
define('MQ_VHOST', UsageConfig::env('MQ_VHOST'));
define('MQ_EXCHANGE_NAME', UsageConfig::env('MQ_EXCHANGE_NAME'));
define('MQ_QUEUE_NAME', UsageConfig::env('MQ_QUEUE_NAME'));

//If this is enabled you can see AMQP output on the CLI
define('AMQP_DEBUG', UsageConfig::env('AMQP_DEBUG'));

abstract class UsageConst {
    const JUMP_FLAG = '#__jump_url__';
    const JQUERY_FLAG = 'jquery';
    const SDK_FLAG = '/sdk/usage.js';
    const JQUERY_SCRIPT = '<script src="/frontend/assets/libs/jquery-1.8.3.min.js" type="text/javascript"></script>';
    const JQUERY_CONFLICT = '<script>jQuery.noConflict();</script>';
    const SDK_SCRIPT = '<script src="/sdk/usage.js?v=19" type="text/javascript"></script>';
    const SDK_TAG = '</head>';
    const REQUEST_TIME = 'getPageRequestTimestamp()';
    const FAILED = 'failed';
    const SUCCESS = 'success';
    const PRODUCT_BASE = 'base';
    const SESSION_NAME = 'REAL_SESSION';
    const EVENT_ERROR = '__event_error__';
    const EVENT_TIME = '__event_time__';
    const ERROR_MIN_TIME = 3600;
    const ACTION_PAGE_VIEW = 'page_view';
    const ACTION_USER_LOGIN = 'user_login';
    const ACTION_USER_LOGOUT = 'user_logout';
    const TYPE_OTHER = 'other';
    const TYPE_SIMPLE_RESULT = 'simple_result';
    const TYPE_ADVANCED_RESULT = 'advanced_result';
    const TYPE_LAW_CONTENT = 'law_content';
    static $EVENT_NAME_MAPPING = array(
        'topic.landing' => array('tps'),
        'topic'         => array('tps')
    );
}

class UsageConfig {
    /**
     * Gets the value of an environment variable. Supports boolean, empty and null.
     *
     * @param  string  $key
     * @param  mixed   $default
     * @return mixed
     */
    public static function env($key, $default = null)
    {
        $self = UsageUtility::instance();
        $value = getenv($key);

        if ($value === false) {
            return $self->value($default);
        }

        switch (strtolower($value)) {
            case 'true':
            case '(true)':
                return true;

            case 'false':
            case '(false)':
                return false;

            case 'empty':
            case '(empty)':
                return '';

            case 'null':
            case '(null)':
                return;
        }

        if ($self->startsWith($value, '"') && $self->endsWith($value, '"')) {
            return substr($value, 1, -1);
        }

        return $value;
    }
}

class UsageUtility {
    private static $obj;
    public static function instance() {
        if (!self::$obj) {
            self::$obj = new self();
        }
        return self::$obj;
    }
    /**
     * Return the default value of the given value.
     *
     * @param  mixed  $value
     * @return mixed
     */
    public function value($value)
    {
        return $value instanceof Closure ? $value() : $value;
    }

    /**
     * Determine if a given string starts with a given substring.
     *
     * @param  string  $haystack
     * @param  string|array  $needles
     * @return bool
     */
    public function startsWith($haystack, $needles)
    {
        foreach ((array) $needles as $needle) {
            if ($needle != '' && substr($haystack, 0, strlen($needle)) === (string) $needle) {
                return true;
            }
        }

        return false;
    }

    /**
     * Determine if a given string ends with a given substring.
     *
     * @param  string  $haystack
     * @param  string|array  $needles
     * @return bool
     */
    public function endsWith($haystack, $needles)
    {
        foreach ((array) $needles as $needle) {
            if (substr($haystack, -strlen($needle)) === (string) $needle) {
                return true;
            }
        }

        return false;
    }
}
